package com.example.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.BeautyCare;
import com.example.demo.entity.DTO.WorkersDTO;
import com.example.demo.entity.Role;
import com.example.demo.entity.Workers;
import com.example.demo.mapper.BeautyCareMapper;
import com.example.demo.mapper.RoleMapper;
import com.example.demo.mapper.WorkersMapper;
import com.example.demo.service.IWorkersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author Mr.wyy
 * @since 2024-05-30
 */
@Service
public class WorkersServiceImpl extends ServiceImpl<WorkersMapper, Workers> implements IWorkersService {

    @Autowired
    private WorkersMapper workersMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private BeautyCareMapper beautyCareMapper;

    @Override
    public List<WorkersDTO> selectByCriteria(Workers workers) {
        return getAllRoleAndBeautyCare(workersMapper.selectByCriteria(workers));
    }

/*    @Override
    public List<Workers> selectAllWorkers() {
        return workersMapper.selectAllWorkers();
    }*/

    private List<WorkersDTO> getAllRoleAndBeautyCare(List<Workers> workersList){
        List<WorkersDTO> workersDTOS = new ArrayList<>();
        for (Workers workers : workersList) {
            // 创建一个WorkersAndBookedTimeDTO实例，用于存储工人和预订时间的信息
            WorkersDTO workersDTO = new WorkersDTO();

            // 将workersAndBookedTime对象的属性值复制到workersAndBookedTimeDTO对象中
            BeanUtil.copyProperties(workers, workersDTO);

            // 获取工人的角色信息 并存入DTO
            Role role = roleMapper.selectRoleByWorkersId(workersDTO.getWorkersId());
            workersDTO.setRole(role);

            // 获取工人的护理信息 并存入DTO
            BeautyCare beautyCare = beautyCareMapper.selectByWorkersId(workersDTO.getWorkersId());
            workersDTO.setBeautyCare(beautyCare);
            // 将workersAndBookedTimeDTO对象添加到list中
            workersDTOS.add(workersDTO);

        }
        // 返回list
        return workersDTOS;
    }
}
